package org.zhadoop.stream.cql.semanticanalyzer.parser.context;

import java.util.List;

import org.zhadoop.stream.cql.DriverContext;
import org.zhadoop.stream.cql.exception.CQLException;
import org.zhadoop.stream.cql.exception.SemanticAnalyzerException;
import org.zhadoop.stream.cql.hooks.SemanticAnalyzeHook;
import org.zhadoop.stream.cql.semanticanalyzer.SelectClauseAnalyzer;
import org.zhadoop.stream.cql.semanticanalyzer.SemanticAnalyzer;
import org.zhadoop.stream.cql.semanticanalyzer.parsecontextwalker.ParseContextWalker;
import org.zhadoop.stream.cql.tasks.Task;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;

/**
 * group by 子语句解析内容
 * 
 * @author zwd
 *
 */
public class GroupbyClauseContext extends ParseContext {

	private List<ExpressionContext> expressions;

	public GroupbyClauseContext() {
		expressions = Lists.newArrayList();
	}

	@Override
	public String toString() {
		return "GROUP BY " + Joiner.on(", ").join(expressions);
	}

	@Override
	public Task createTask(DriverContext driverContext, List<SemanticAnalyzeHook> analyzeHooks) throws CQLException {
		return null;
	}

	@Override
	public SemanticAnalyzer createAnalyzer() throws SemanticAnalyzerException {
		SelectClauseContext selectContext = new SelectClauseContext();
		for(ExpressionContext exp : expressions){
			SelectExpressionContext selectExp = new SelectExpressionContext();
			selectExp.setExpression(exp);
			
			SelectItemContext item = new SelectItemContext();
			item.setExpression(selectExp);
			selectContext.getSelectItems().add(item);
		}
		return new SelectClauseAnalyzer(selectContext);
	}

	@Override
	protected void walkChild(ParseContextWalker walker) {
		for (ExpressionContext exp : expressions) {
			walkExpression(walker, exp);
		}
	}

	public List<ExpressionContext> getExpressions() {
		return expressions;
	}

	public void setExpressions(List<ExpressionContext> expressions) {
		this.expressions = expressions;
	}

}
